package com.noassertions.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.noassertions.data.DAOInterface;
import com.noassertions.domain.User;

/**
 * Collection of controllers to map requests to static content
 * Maybe we should dump this static content outside of spring?
 * @author Matt Phillips, mphillip@vt.edu
 */

@Controller
public class StaticContentController
{
	
	private DAOInterface dao;

    @Autowired
    public StaticContentController(DAOInterface dao) {
        this.dao = dao;
    } 
	
	@RequestMapping(value = "/measure", method = RequestMethod.GET)
	public String getMeasure(Model model)
	{		
		// TODO this business of checking for a string (which indicates that user is not authenticated) is a total hack and should be factored out
		if (! (SecurityContextHolder.getContext().getAuthentication().getPrincipal() instanceof String)) {
			UserDetails principal = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			User user = dao.getUserByemailAddress(principal.getUsername());
			model.addAttribute("emailAddress", user.getEmailaddress());
		}
		return "static/measure";
	}	
	
	@RequestMapping(value = "/measure/print", method = RequestMethod.GET)
	public String getMeasurePrinter(Model model)
	{		
		// TODO this business of checking for a string (which indicates that user is not authenticated) is a total hack and should be factored out
		if (! (SecurityContextHolder.getContext().getAuthentication().getPrincipal() instanceof String)) {
			UserDetails principal = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			User user = dao.getUserByemailAddress(principal.getUsername());
			model.addAttribute("emailAddress", user.getEmailaddress());
		}
		return "static/measureprinter";
	}	
	
	@RequestMapping(value = "/contact", method = RequestMethod.GET)
	public String getContact(Model model)
	{		
		// TODO this business of checking for a string (which indicates that user is not authenticated) is a total hack and should be factored out
		if (! (SecurityContextHolder.getContext().getAuthentication().getPrincipal() instanceof String)) {
			UserDetails principal = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			User user = dao.getUserByemailAddress(principal.getUsername());
			model.addAttribute("emailAddress", user.getEmailaddress());
		}
		return "static/contact";
	}	
	
	@RequestMapping(value = "/license", method = RequestMethod.GET)
	public String geLicense(Model model)
	{		
		// TODO this business of checking for a string (which indicates that user is not authenticated) is a total hack and should be factored out
		if (! (SecurityContextHolder.getContext().getAuthentication().getPrincipal() instanceof String)) {
			UserDetails principal = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			User user = dao.getUserByemailAddress(principal.getUsername());
			model.addAttribute("emailAddress", user.getEmailaddress());
		}
		return "static/license";
	}	
	
	@RequestMapping(value = "/faq", method = RequestMethod.GET)
	public String getFaq(Model model)
	{	
		// TODO this business of checking for a string (which indicates that user is not authenticated) is a total hack and should be factored out
		if (! (SecurityContextHolder.getContext().getAuthentication().getPrincipal() instanceof String)) {
			UserDetails principal = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			User user = dao.getUserByemailAddress(principal.getUsername());
			model.addAttribute("emailAddress", user.getEmailaddress());
		}
		return "static/faq";
	}	
}